//
//  UserViewController.m
//  Travel
//
//  Created by 吴品泽 on 15/4/11.
//  Copyright (c) 2015年 &#21556;&#21697;&#27901;. All rights reserved.
//

#import "UserViewController.h"
#import <AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>
#import "DetailViewController.h"

#import "UserModel.h"
#import "UserTripsModel.h"
#import "TravelTableViewCell.h"
#import "UserTableViewCell.h"
#import "PhotoModel.h"
#import "MJRefresh.h"

#import "ImageWaterView.h"
@interface UserViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain)UIView *buttonView;
@property (nonatomic,retain)UIButton *leftButton;
@property (nonatomic,retain)UIButton *rightButton;
@property (nonatomic,retain)UIView *backView;

@property (nonatomic,strong)ImageWaterView *waterView;

@property (nonatomic,retain)UIView *view1;
@property (nonatomic,retain)UIView *view2;


@property (nonatomic,retain)UITableView *tableview1;
@property (nonatomic,retain)NSMutableArray *photosArr;

@property (nonatomic,strong)UserModel *modes;
@property (nonatomic,assign)NSInteger shu;
@property (nonatomic,strong)NSMutableArray *tableArr;

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma tableView的下拉加载还没做
    // Do any additional setup after loading the view.
   
   
    self.tableArr = [NSMutableArray array];
    self.title = self.gaTitle;
    [self requestTableViewData];
    
  //  [self createTableView];
}

//上拉加载下拉刷新
- (void)createRefreash
{
    [self.tableview1 addHeaderWithTarget:self action:@selector(headRefreshing)];
    [self.tableview1 addFooterWithTarget:self action:@selector(footeRefreshing)];
   // [self.tableview1 headerBeginRefreshing];
    
    self.tableview1.headerPullToRefreshText = @"下拉刷新";
    self.tableview1.headerReleaseToRefreshText = @"松开马上刷新了";
    self.tableview1.headerRefreshingText = @"正在刷新中,请稍后";
    
    
    self.tableview1.footerPullToRefreshText = @"上拉加载更多数据";
    self.tableview1.footerReleaseToRefreshText = @"松开加载更多数据";
    self.tableview1.footerRefreshingText = @"正在加载中。。。";
    
}

- (void)headRefreshing
{
    //自动刷新
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableview1 reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableview1 headerEndRefreshing];
    });
    
}

- (void)footeRefreshing
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableview1 reloadData];
        NSString *string = [NSString stringWithFormat:@"%ld",(long)self.shu];
        NSString *str = [NSString stringWithFormat:@"https://chanyouji.com/api/users/%@.json?page=%@",self.userID,string];
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        [manager GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
            UserModel *no = [[UserModel alloc]init];
            [no setValuesForKeysWithDictionary:responseObject];
            [no getTripModel];
            
            for (UserTripsModel *demo in no.trips) {
            
                [self.modes.trips addObject:demo];
            }
            
            
            [self.tableview1 reloadData];
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // NSLog(@"error: %@",error);
        }];
        
        
        [self.tableview1 footerEndRefreshing];
        self.shu += 1;
    });
    
    
}







- (void)requestForLikeImageView
{
     self.photosArr = [NSMutableArray array];
    NSString *strs = [NSString stringWithFormat:@"https://chanyouji.com/api/users/likes/%@.json?per_page=18&page=1",self.userID];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strs parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"%@",responseObject);
        for (NSMutableDictionary *dics in responseObject) {
            PhotoModel *mod = [[PhotoModel alloc]init];
            [mod setValuesForKeysWithDictionary:dics];
            [self.photosArr addObject:mod];
        }
        
        NSLog(@"<<<<<<<<<<<<%ld",self.photosArr.count);
        self.waterView = [[ImageWaterView alloc]initWithDataArray:self.photosArr withFrame:CGRectMake(0, 0, self.view2.frame.size.width, self.view2.frame.size.height)];
      //  self.waterView.backgroundColor = [UIColor yellowColor];
        [self.view2 addSubview:self.waterView];
        //[self hehe];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求出错了");
    }];
    
   
}




- (void)createUserInfomationAndTwoButton
{
    UIView *bigView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 104)];
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, bigView.frame.size.width, 64)];
    UIImageView *imagView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 44 , topView.frame.size.height - 20)];
    
    imagView.layer.masksToBounds = YES;
    imagView.layer.cornerRadius = 22;
    
#pragma 用户的占位图没添加
    [imagView sd_setImageWithURL:[NSURL URLWithString:self.modes.image] placeholderImage:nil];
  //  imagView.backgroundColor = [UIColor greenColor];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(64, 10, WIDTH - 54, 30)];
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(64, 40, WIDTH - 54, 20)];
  //  label1.backgroundColor = [UIColor redColor];
    label2.font = [UIFont systemFontOfSize:15];
    NSString *label2Str = [NSString stringWithFormat:@"%@ 篇游记",self.modes.trips_count];
    label2.text = label2Str;
    label1.text = self.gaTitle;
    
    //按钮的底部视图
    self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(0,64,WIDTH, 40)];
    self.buttonView.userInteractionEnabled = YES;
  //  self.buttonView.backgroundColor = [UIColor yellowColor];
    UIView  *underButtonView = [[UIView alloc]initWithFrame:CGRectMake(10, 5, WIDTH - 20, self.buttonView.frame.size.height - 10)];
    underButtonView.layer.masksToBounds = YES;
    underButtonView.layer.cornerRadius = 5;
    underButtonView.userInteractionEnabled = YES;
    //左侧按钮的设置
    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.leftButton.frame = CGRectMake(0, 0, underButtonView.frame.size.width / 2, underButtonView.frame.size.height);
    [self.leftButton setTitle:@"游记" forState:UIControlStateNormal];
    //self.leftButton.layer.masksToBounds = YES;
    // self.leftButton.layer.cornerRadius = 5;
    
    //右侧按钮的设置
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.rightButton.frame = CGRectMake(underButtonView.frame.size.width / 2, 0, underButtonView.frame.size.width / 2, underButtonView.frame.size.height);
    [self.rightButton setTitle:@"喜欢" forState:UIControlStateNormal];
    
    self.rightButton.tintColor = [UIColor blackColor];
    self.leftButton.tintColor = [UIColor colorWithRed:153 / 255.0 green:204 / 255.0 blue:255 / 255.0 alpha:1];;
    
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(1, 1, underButtonView.frame.size.width / 2 - 1, underButtonView.frame.size.height - 2)];
    self.backView.userInteractionEnabled = YES;
    self.backView.layer.masksToBounds = YES;
    self.backView.layer.cornerRadius = 5;
    
    self.backView.backgroundColor = [UIColor whiteColor];
    underButtonView.backgroundColor = [UIColor colorWithRed:230 / 255.f green:230 / 255.f blue:230 / 255.f alpha:1];
    
    // self.buttonView.backgroundColor = [UIColor yellowColor];
    //self.leftButton.backgroundColor = [UIColor purpleColor];
    // self.rightButton.backgroundColor = [UIColor redColor];
    // self.leftButton.alpha = 0;
    //self.rightButton.alpha = 0;
    
    
    
    [self.leftButton addTarget:self action:@selector(appreView1:)forControlEvents:UIControlEventTouchUpInside];
    
    [self.rightButton addTarget:self action:@selector(appreView2:)forControlEvents:UIControlEventTouchUpInside];
    
    
    [bigView addSubview:self.buttonView];
    [self.buttonView addSubview:underButtonView];
    [underButtonView addSubview:self.backView];
    [underButtonView addSubview:self.leftButton];
    [underButtonView addSubview:self.rightButton];
    
    [bigView addSubview:topView];
    [topView addSubview:imagView];
    [topView addSubview:label1];
    [topView addSubview:label2];
    
    [self.view addSubview:bigView];
}



- (void)appreView1:(UIButton *)leftbutton
{
    // NSLog(@"111");
    self.rightButton.tintColor = [UIColor blackColor];
    self.leftButton.tintColor = [UIColor colorWithRed:153 / 255.0 green:204 / 255.0 blue:255 / 255.0 alpha:1];
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.center = self.leftButton.center;
    }];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.view2.frame = CGRectMake(WIDTH, 168, WIDTH, HIGHT - 168);
        self.view1.frame = CGRectMake(0, 168, WIDTH, HIGHT - 168);
    }];
}


- (void)appreView2:(UIButton *)rightbutton
{
    //  NSLog(@"222%f",self.tabBarController.tabBar.frame.size.heigh
    self.rightButton.tintColor = [UIColor colorWithRed:153 / 255.0 green:204 / 255.0 blue:255 / 255.0 alpha:1];
    self.leftButton.tintColor = [UIColor blackColor];
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.center = self.rightButton.center;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.view1.frame = CGRectMake(- WIDTH, 168, WIDTH, HIGHT - 168 );
            self.view2.frame = CGRectMake(0, 168, WIDTH, HIGHT - 168 );
        }];
        
    }];
}



- (void)createView1AndView2
{
    self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 168, WIDTH, HIGHT - 168)];
   // self.view1.backgroundColor = [UIColor purpleColor];
    
    
    self.view2 = [[UIView alloc]initWithFrame:CGRectMake(WIDTH, 168, WIDTH, HIGHT - 168 )];
    // self.view2.backgroundColor = [UIColor redColor];
    
    
    [self.view addSubview:self.view1];
    [self.view addSubview:self.view2];
    
}


- (void)createTableView
{
    self.tableview1 = [[UITableView alloc]initWithFrame:self.view1.bounds style:UITableViewStylePlain];
    self.tableview1.delegate = self;
    self.tableview1.dataSource = self;
    //  [self.tableview1 registerNib:[UINib nibWithNibName:@"TravelCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"travelCell"];
    self.tableview1.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view1 addSubview:self.tableview1];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modes.trips.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"hehess";
    UserTableViewCell *cell = [self.tableview1 dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[UserTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UserTripsModel *notes = [self.modes.trips objectAtIndex:indexPath.row];
    cell.notes = notes;
    
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // self.traveCell = (TravelCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    
    return 160;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserTripsModel *note = [self.modes.trips objectAtIndex:indexPath.row];
    DetailViewController *detal = [[DetailViewController alloc]init];
    detal.hidesBottomBarWhenPushed = YES;
    detal.IDNumber = note.ID;
    [self.navigationController pushViewController:detal animated:YES];
}

- (void)requestTableViewData
{
    NSString *str = [NSString stringWithFormat:@"https://chanyouji.com/api/users/%@.json?page=1",self.userID];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.modes = [[UserModel alloc]init];
        [self.modes setValuesForKeysWithDictionary:responseObject];
        
        [self.modes getTripModel];
        
      //  NSLog(@"%@ ",self.modes);
       // NSLog(@"%@",self.modes.trips);
        
        [self createUserInfomationAndTwoButton];
        [self createView1AndView2];
        [self createTableView];
         self.shu = 2;
        [self createRefreash];
        [self requestForLikeImageView];
       
        [self.tableview1 reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"网络出错了");
        
    }];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
